<?php

/**
 * Description of IndexController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Encheres\Frontend\Controller;

use Library\BackController;
use Library\HttpRequest;
use Library\Tools;

class EncheresController extends BackController{
    
    public function executeIndex(HttpRequest $request){
        // On ajoute une définition pour le titre
        //$this->page->addVar('title', 'Liste des 5 dernières news');
        $manager = $this->managers->getManagerOf('Encheres');
        
        $enchersList = $manager->getEnchereUne();
        
    }
    
    /**
     *On génére l'encheres avenir
     * @param HttpRequest $request 
     */
    public function executeBecome(HttpRequest $request){
        // On ajoute une définition pour le titre
        $this->page->addVar('title', 'Enchères à venir');
        $manager = $this->managers->getManagerOf('Encheres');
        $enchersList = $manager->getEncheresAvenir();
        $listenchere = array();
        foreach ($enchersList as $key => $value) {
            $out = array();
            $out['pc']=$value->getLimite()==0?'b':'';
            $out['ae']=$value->getAe()==0?'b':'';
            $out['AE'] = 'AE';
            $val = $value->getRetime();
            $retime =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
            $out['retime'] = $retime;
            $out['limite'] = $value->getLimite();
            $out['nom'] = $value->getNom();
            $out['id'] = $value->getId();
            $img = $value->getImages();
            if(!empty($img)){
                $img = explode(';', $value->getImages());
                $out['images'] = $img[0];
            }
            $listenchere[] = $out;
        }
        $this->page->addVar('enchersList', $listenchere);
    }
    
    /**
     *On génére l'encheres terminée
     * @param HttpRequest $request 
     */
    public function executeFinish(HttpRequest $request){
        // On ajoute une définition pour le titre
        $this->page->addVar('title', 'Enchères Terminées');
        $manager = $this->managers->getManagerOf('Encheres');
        $enchersList = $manager->getEncheresTerminees();
        $listenchere = array();
        foreach ($enchersList as $key => $value) {
            $out = array();
            $out['pc']=$value->getLimite()==0?'b':'';
            $out['ae']=$value->getAe()==0?'b':'';
            $out['AE'] = 'AE';
            $val = $value->getRetime();
            $retime =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
            $out['retime'] = $retime;
            $out['limite'] = $value->getLimite();
            $out['nom'] = $value->getNom();
            $out['id'] = $value->getId();
            $img = $value->getImages();
            if(!empty($img)){
                $img = explode(';', $value->getImages());
                $out['images'] = $img[0];
            }
            $out['pseudo'] = (($value->getPseudo()!=null)?$value->getPseudo():'Aucun');
            $out['enchere'] = str_replace('.', ',', $value->getEnchere());
            $out['prix'] = $value->getPrix();
            $listenchere[] = $out;
        }
        $this->page->addVar('enchersList', $listenchere);
    }
    
    /**
     * description de l'enchere
     * @param HttpRequest $request 
     */
    public function executeShow(HttpRequest $request){
        //var_dump($request->getData('id')); die();
        $detail = $this->managers->getManagerOf('Encheres')->getEnchereById($request->getData('id'));
        $this->page->addVar('title', 'Description de l\'enchères');
        $this->page->addVar('nbrcolumn', 1);
        $this->page->addVar('detailObj', $detail);
        $tools = new Tools();
        foreach ($detail as $value) {
           $pc=$value->getLimite()==0?'b':'';
           $ae=$value->getAe()==0?'b':'';
           $value->setAe('AE');
           $this->page->addVar('pc', $pc);
           $this->page->addVar('ae', $ae);
           $this->page->addVar('description', html_entity_decode($value->getDescription()));
           $img = $value->getImages();
           if(!empty($img)){
               $tabimg = explode(';', $value->getImages());
               $value->setImages(array_shift($tabimg));
               array_pop($tabimg);
               $this->page->addVar('tabimg', $tabimg);
           }
           $value->setEnchere(str_replace('.', ',', $value->getEnchere()));
           list($datee, $heuree) = explode(' ', $value->getDateFin());
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $time_en = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0])) - time();
           //$time_en = ($value->getDateFin() - time());
           $type = ($value->getTerminee()==1 || $time_en<0?'termine':($time_en==$value->getDuree()?'avenir':'encours'));
           $this->page->addVar('type', $type);
           $time_en = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_;
           $this->page->addVar('timeEn', $time_en);
           //gestion du temps
           $val = $value->getRetime();
           $retime =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
           $value->setRetime($retime);
           //gestion du meneur
           $value->setPseudo((($value->getPseudo()!=null)?$value->getPseudo():'Aucun'));
           $this->page->addVar('historique', $this->managers->getManagerOf('Encheres')->getEnrichisseurs($value->getId()));
        }
    }
    /**
     * fonction permettant de faire des traitement ajax
     * @param HttpRequest $request 
     */
    public function executeAjaxT(HttpRequest $request){
        if($request->isXmlHttpRequest() && isset($_SESSION['user']) && $_SESSION['user']['credits'] !=0){
            $manager = $this->managers->getManagerOf('Encheres');
            $arraydata =array();
            $arraydata['iduser'] = $request->postData('iduser');
            $arraydata['idprod'] = $request->postData('idprod');
            $enchere = $manager->getEnchereById($arraydata['idprod']);
            foreach ($enchere as $value) {
                list($datee, $heuree) = explode(' ', $value->getDateFin());
                $datee = explode('-', $datee);
                $heuree = explode(':', $heuree);
                $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0])) - time();
                if($datef < $value->getRetime()){
                    $arraydata['addtime'] = date('Y-m-d H:i:s',time()+intval($value->getRetime()));
                }else{
                    $arraydata['addtime'] = $value->getDateFin();
                }
            }
            $manager->enrichisseurs($arraydata);
            //mise à jour du crédit de l'utilisateur
            if($this->managers->getManagerOf('Users')->updateCreditDecrement($arraydata)){
                $_SESSION['user']['credits'] = intval($_SESSION['user']['credits'])-1;
            }
        }
        exit();
    }
}

?>
